<template>
  <view>
    <muscihead title="登录" :icon="true"></muscihead>
    <view class="card">
      <uni-card :is-shadow="true">
        <!-- 电话 -->
        <view class="nubber">
          电话号码：
          <view class="inppt">
            <u--input
              placeholder="请输入电话"
              border="surround"
              v-model="value"
              clearable
              type="number"
            ></u--input>
          </view>
        </view>
        <!-- 验证码 -->
        <view class="nubber">
          密码:
          <view class="inppt">
            <u--input
              placeholder="请输入密码"
              border="surround"
              v-model="code"
              clearable
              type="password"
            ></u--input>
          </view>
        </view>
        <!-- 按钮 -->
        <view class="button">
          <u-button type="primary" text="登录" @click="enter()"></u-button>
          <u-button type="primary" text="取消" @click="quit()"></u-button>
        </view>
      </uni-card>
    </view>
  </view>
</template>

<script>
import "@/common/iconfont.css";
import muscihead from "../../components/musichead/musichead";
import { baseUrl } from "../../common/config";
export default {
  components: {
    muscihead,
  },
  props: {},
  data: () => ({
    tips: "",
    value: "",
    code: "",
    cookieToken: "",
    userinfo: {},
  }),
  computed: {},
  methods: {
    //登录接口
    enter() {
      if (!this.value || !this.code) {
        const msg = "请设置你的手机号码和密码";
        alert(msg);
        throw new Error(msg);
      }
      uni.request({
        url: `${baseUrl}/login/cellphone?phone=${
          this.value
        }&password=${encodeURIComponent(this.code)}`,
        success: (res) => {
          if (res.data.code === 200) {
            console.log(res.data);
            this.cookieToken = res.data.cookie;
            this.userinfo = res.data.profile;
            console.log(this.userinfo);
            console.log(this.cookieToken);
            getApp().globalData.userInfo = this.userinfo;
            uni.setStorage({
              key: "user",
              data: JSON.stringify(this.userinfo),
            });
              uni.setStorage({
              key: "cookie",
              data:this.cookieToken,
            });
            uni.navigateTo({
              url: "/pages/index/index",
            });
          } else {
            uni.showToast({
              title: "账号或密码错误",
              icon: "none",
              duration: 2000,
            });
          }
        },
      });
    },
    quit() {
      uni.navigateBack({
         delta: 1
      });
      // uni.request({
      //   url: `${baseUrl}/login/status?cookie=${encodeURIComponent(
      //     this.cookieToken
      //   )}`,
      //   success: (res) => {
      //     console.log(res.data);
      //   },
      // });
    },
    codeChange(text) {
      this.tips = text;
      console.log(this.tips);
    },
  },
  watch: {},
};
</script>

<style lang="scss" scoped>
.card {
  margin-top: 300rpx;
}
.nubber {
  display: flex;
  justify-content: center;
  align-items: center;
  //  width: 600rpx;
  .inppt {
    width: 400rpx;
  }
}
.code {
  margin-top: 20rpx;
  margin-bottom: 30rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  .inppts {
    width: 400rpx;
  }
  // width: 600rpx;
}

.button {
  display: flex;
  justify-content: center;
  align-content: center;
}
</style>
